package com.chalk.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.chalk.common.util.Result;
import com.chalk.exception.ColumnNotEmptyException;
import com.chalk.model.Venue;
import com.chalk.model.VenueAndSe;
import com.chalk.service.SportEventService;
import com.chalk.service.VenueAndSeService;
import com.chalk.service.VenueService;
import com.chalk.vo.VenueVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author glq
 * @since 2020-12-10
 */

@RestController
@RequestMapping("/v1/venue")
public class VenueController {
    @Autowired
    VenueService venueService;

    @Autowired
    VenueAndSeService venueAndSeService;
    @Autowired
    SportEventService sportEventService;

    /**
     * @Author 86130
     * @Description //todo 场馆分页
     * @methodName getVenueForPage
     * @param map: 分页信息
     * @return Result 返回结果类
     * @Date  2021/9/27 19:05
     **/
    @GetMapping("getVenueForPage")
    public Result getVenueForPage(@RequestParam Map<String, Object> map) throws ColumnNotEmptyException {
        Page<Venue> venuePage = venueService.getVenueForPage(map);
        venuePage.getRecords().forEach(venue -> {
            venue.setSportEvents(sportEventService.getSportEvents(venueAndSeService.getSportEventsByVenueId(venue.getVenueId())));
        });
        return Result.success(venuePage);
    }

   /**
    * @Author 86130
    * @Description //todo 添加场馆
    * @methodName saveVenue
    * @param venueVo: 场馆包装类
    * @return Result
    * @Date  2021/9/27 19:06
    **/
    @PostMapping("saveVenue")
    public Result saveVenue(@RequestBody VenueVo venueVo) {
        if (venueVo.getSeIds().size() == 0) {
            return Result.fail("运动项目id不能为空！");
        }
        Boolean f = venueService.saveVenue(venueVo);
        return f ? Result.success("操作成功！") : Result.fail("操作失败");
    }

    /**
     * @Author 86130
     * @Description //todo 修改场馆
     * @methodName updateVenue
     * @param venueVo: 场馆包装类
     * @return Result
     * @Date  2021/9/27 19:08
     **/
    @PostMapping("updateVenue")
    public Result updateVenue(@RequestBody VenueVo venueVo) {
        if (venueVo.getVenueId() == null) {
            return Result.fail("场馆id不能为空！");
        }
        if (venueVo.getSeIds().size() == 0) {
            return Result.fail("项目id不能为空！");
        }
        Boolean f = venueService.updateVenue(venueVo);
        return f ? Result.success("操作成功！") : Result.fail("操作失败");
    }

    /**
     * @Author 86130
     * @Description //todo 删除场馆
     * @methodName deleteVenue
     * @param venueIds: 场馆
     * @return Result
     * @Date  2021/9/27 19:10
     **/
    @DeleteMapping("deleteVenue")
    public Result deleteVenue(@RequestBody Integer[] venueIds) {
        System.out.println("venueIds为"+venueIds);
        if (venueIds == null) {
            return Result.fail("场馆id不能为空！");
        }
        Boolean f = venueService.deleteVenue(venueIds);
        return f ? Result.success("操作成功！") : Result.fail("操作失败");
    }

    /**
     * 场馆设为热推  /取消热推
     * @param venueId
     * @return
     */
    @PutMapping("/setIsRecommend")
    public Result setIsRecommend(Integer venueId) {
        System.out.println("venueId为："+venueId);
        if(venueId==null){
            return Result.fail("场馆id不能为空！");
        }
        Boolean f = venueService.setIsRecommend(venueId);
        return f ? Result.success("操作成功！") : Result.fail("操作失败");
    }

}

